import requests
import json

http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
key = "wmhpi-wshbWHM4j3Q7T4d5y3DVHtOHxU"
secret = "5IdwuPK3SvBoUTa8ofwZKJf3QnvP6bDI"
filepath = "D:/项目/python/jieping.jpg"

data = {
    "api_key": key,
    "api_secret": secret,
    "return_attributes": "gender,age,smiling,beauty",
}

try:
    # 当使用 with 打开文件时，Python 会在进入和退出代码块时自动调用文件对象的 __enter__() 和 __exit__() 方法。这意味着当你离开 with 代码块时，无论代码是否执行成功，文件都会被自动关闭。这减少了手动管理文件关闭的负担，并降低了因忘记关闭文件而导致的问题风险，如文件描述符泄漏
    with open(filepath, "rb") as image_file:
        files = {"image_file": image_file}
        response = requests.post(http_url, data=data, files=files)
        req_con = response.content.decode("utf-8")
        req_dict = json.loads(req_con)
        print(req_dict)
except Exception as e:
    print(f"发生错误: {e}")
